package pswvclass.pswvandroid2.com;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.gms.common.api.GoogleApiClient;

import java.io.File;

import pswvclass.pswvandroid2.R;
import pswvclass.pswvandroid2.com.category.CategoryList;
import pswvclass.pswvandroid2.com.kwords.KwordList;
import pswvclass.pswvandroid2.com.more.MoreMenu;
import pswvclass.pswvandroid2.com.need.Goal;
import pswvclass.pswvandroid2.com.risk.RiskAdd;
import pswvclass.pswvandroid2.com.risk.RiskList;
import pswvclass.pswvandroid2.com.util.HandleBackInterface;
import pswvclass.pswvandroid2.com.util.HandleBackUtil;
import pswvclass.pswvandroid2.com.util.UpdateManager;


/**
 * Created by Administrator on 2018/11/1.
 */
public class PswvActivity extends AppCompatActivity implements View.OnClickListener {
    private Toolbar toolbar;
    private Button home_page;
    private Button kword_page;
    private Button need_page;
    private Button my_page;
    public static int version,serverVersion;
    public static String versionName,serverVersionName,downloadResult;
    public static receiveVersionHandler handler;
    private ProgressBar proBar;
    private UpdateManager manager = UpdateManager.getInstance();
    private Fragment fragment;
    public static int exitTime = 0;
    private static int b = 1;
    long startTime = 0;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_pswv_index);
        toolbar = (Toolbar) findViewById(R.id.toolbar);

        /*Context c = this;
        version = manager.getVersion(c);
        versionName = manager.getVersionName(c);
        handler = new receiveVersionHandler();
        manager.compareVersion(PswvActivity.this);*/
        /**
         * 设置标题
         */
        toolbar.setTitle(" ");
        /**
         * 设置子标题
         */
        toolbar.setSubtitle(" ");
        setSupportActionBar(toolbar);
        my_page = (Button) findViewById(R.id.my_page);
        my_page.setOnClickListener(this);
        home_page = (Button) findViewById(R.id.risk_page);
        home_page.setOnClickListener(this);
        kword_page = (Button) findViewById(R.id.kword_page);
        kword_page.setOnClickListener(this);
        need_page = (Button) findViewById(R.id.need_page);
        need_page.setOnClickListener(this);
        int id = getIntent().getIntExtra("id", 0);
        if (id == 4) {
            exitTime = 1;
            View cv = getWindow().getDecorView();
            Resources resources = cv.getContext().getResources();
            my_page.setBackground(resources.getDrawable(R.drawable.ditu));
            home_page.setVisibility(cv.INVISIBLE);
            kword_page.setVisibility(cv.INVISIBLE);
            need_page.setVisibility(cv.INVISIBLE);
            FragmentManager fragmentM = getSupportFragmentManager();//管理对象
            android.support.v4.app.FragmentTransaction trans = fragmentM.beginTransaction();//切换碎片
            trans.replace(R.id.realtabcontent, new MoreMenu());
            trans.commit();
        }else {
            FragmentManager fragmentM = getSupportFragmentManager();//管理对象
            android.support.v4.app.FragmentTransaction trans = fragmentM.beginTransaction();//切换碎片
            trans.replace(R.id.realtabcontent, new Home());
            trans.commit();
        }
    }



    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.my_page:{
                exitTime = 1;
                Resources resources = view.getContext().getResources();
                my_page.setBackground(resources.getDrawable(R.drawable.ditu));
                home_page.setVisibility(view.INVISIBLE);
                kword_page.setVisibility(view.INVISIBLE);
                need_page.setVisibility(view.INVISIBLE);
                FragmentManager fragmentM = getSupportFragmentManager();//管理对象
                android.support.v4.app.FragmentTransaction trans = fragmentM.beginTransaction();//切换碎片
                trans.replace(R.id.realtabcontent, new MoreMenu());
                trans.commit();
            }
                break;
            case R.id.risk_page:{
                exitTime = 1;
                Resources resources = view.getContext().getResources();
                home_page.setBackground(resources.getDrawable(R.drawable.ditu));
                my_page.setVisibility(view.INVISIBLE);
                kword_page.setVisibility(view.INVISIBLE);
                need_page.setVisibility(view.INVISIBLE);
                FragmentManager fragmentM = getSupportFragmentManager();//管理对象
                android.support.v4.app.FragmentTransaction trans = fragmentM.beginTransaction();//切换碎片
                trans.replace(R.id.realtabcontent, new RiskList());
                trans.commit();
            }
                break;
            case R.id.kword_page:{
                exitTime = 1;
                FragmentManager fragmentM1 = getSupportFragmentManager();//管理对象
                android.support.v4.app.FragmentTransaction trans1 = fragmentM1.beginTransaction();//切换碎片
                trans1.replace(R.id.realtabcontent, new KwordList());
                trans1.commit();
                Resources resources1 = view.getContext().getResources();
                my_page.setVisibility(view.INVISIBLE);
                home_page.setVisibility(view.INVISIBLE);
                need_page.setVisibility(view.INVISIBLE);
                kword_page.setBackground(resources1.getDrawable(R.drawable.ditu));
            }
                break;
            case R.id.need_page:{
                exitTime = 1;
                FragmentManager fragmentM2 = getSupportFragmentManager();//管理对象
                android.support.v4.app.FragmentTransaction trans2 = fragmentM2.beginTransaction();//切换碎片
                trans2.replace(R.id.realtabcontent, new Goal());
                trans2.commit();
                Resources resources2 = view.getContext().getResources();
                my_page.setVisibility(view.INVISIBLE);
                home_page.setVisibility(view.INVISIBLE);
                kword_page.setVisibility(view.INVISIBLE);
                need_page.setBackground(resources2.getDrawable(R.drawable.ditu));
            }
                break;
            default:
                break;
        }
    }

  public class receiveVersionHandler extends Handler {
      @Override
      public void handleMessage(Message msg) {
          proBar.setProgress(msg.arg1);
        //  proBar.setVisibility(R.id.button_id);
         // textView.setText("下载进度："+msg.arg1);
          if(msg.arg1 == 100){
              Intent intent = new Intent(Intent.ACTION_VIEW);
              String path = Environment.getExternalStorageDirectory()+"/AndroidUpdateDemo.apk";
              intent.setDataAndType(Uri.fromFile(new File(path)),
                      "application/vnd.android.package-archive");
              startActivity(intent);
          }
          //proBar.setVisibility(R.id.button_id);
      }
  }
}
